package org.pewpew.server;

import org.pewpew.model.Player;

public class GamePhysics {

	public static final double WEAPON_DAMAGE = 100.0;
	public static final double WEAPON_ANGLE = 15.0; // in degrees
	public static final double WEAPON_DISTANCE = 1000.0; //in meters
	
	public static double azimuth (Player user1, Player user2) {
		return GeoMath.azimuth (user2.getLongitude() - user1.getLongitude(), user2.getLatitude() - user1.getLatitude());
	}

	public static double distanceInMeters(Player user1, Player user2) {
		return GeoMath.distance (user1.getLatitude(), user1.getLongitude(), user2.getLatitude(), user2.getLongitude());
	}

	public static double distaceBetween(Player attacker, Player victim) {
		return GeoMath.distance(victim.getLatitude(), victim.getLongitude (), attacker.getLatitude(), attacker.getLongitude ());
	}

	public static double angleBetween(Player attacker, Player target) {
		double azimuth = GeoMath.azimuth(target.getLongitude() - attacker.getLongitude(), target.getLatitude() - attacker.getLatitude());
		double difference = Math.abs(attacker.getOrientation() - azimuth);
		if (difference > 180) {
			difference = 360 - difference;
		}
		return difference;
	}

	public static double calculateDamageFromShoot(Player attacker, Player target) {
		double distance = distaceBetween (attacker, target);
		double angle = angleBetween (attacker, target);
		
		if (angle >= GamePhysics.WEAPON_ANGLE) {
			return 0;
		}
		
		if (distance < 0 || distance >= GamePhysics.WEAPON_DISTANCE) {
			return 0;
		}
		
		double angleWeight = 1 - (angle*angle)/(GamePhysics.WEAPON_ANGLE * GamePhysics.WEAPON_ANGLE);
		double distanceWeight = 1 - (distance*distance)/(GamePhysics.WEAPON_DISTANCE * GamePhysics.WEAPON_DISTANCE);
		return WEAPON_DAMAGE * angleWeight  * distanceWeight;
	}

}
